package com.edu.service.impl;

import cn.hutool.core.convert.Convert;
import com.edu.annotation.EnableLimitPage;
import com.edu.annotation.EnableRedisCache;
import com.edu.mapper.StudentMapper;
import com.edu.myenum.TypeMethod;
import com.edu.pojo.APIRequest;
import com.edu.pojo.Student;
import com.edu.service.StudentService;
import com.edu.util.FtpUtil;
import com.edu.util.IDUtil;
import com.edu.util.RedisClientUtil;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @作者 five-five
 * @创建时间 2021/1/11
 */
@Service
@Transactional
public class StudentServiceImpl implements StudentService {
    @Value("${host}")
    String FTP_URL = null;
    @Value("${port}")
    String FTP_PORT = null;
    @Value("${ftp_password}")
    String FTP_PASSWORD = null;
    @Value("${ftp_user}")
    String FTP_USERNAME = null;
    @Value("${ftp_basePath}")
    String FTP_BASEPATH = null;
    @Value("${ftp_pic_base_url}")
    String FTP_PIC_BASE_URL = null;

    @Autowired
    private RedisClientUtil redisClientUtil;

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;
    //分文件夹用的
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");

    @EnableRedisCache(value = "stus",type = TypeMethod.SELECT)
    @EnableLimitPage
    @Override
    public List<Student> getAll(APIRequest apiRequest) {
        return studentMapper.selectByExample(null);
    }

    @Override
    public int delById(Integer id) {
        return studentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void batcheAddStus(Integer num) {
        Integer integer = Convert.toInt(num);
        List<Student> stus = new ArrayList<>();
        StudentMapper mapper = sqlSessionTemplate.getMapper(StudentMapper.class);
        for (Integer i = 0; i < integer; i++) {
            Student student = new Student();
            student.setId(i);
            student.setName("测试学生" + i + "号");
            student.setSex((i % 2 == 0 ? 0 : 1) + "");
            student.setBirthday(new Date());
            student.setAddress("湖南" + i + "号测试地点");
//            studentMapper.insert(student);
            mapper.insert(student);
        }
    }

    @Override
    public void uploadFile(MultipartFile file) {
//        String host, int port, String username, String password, String basePath,
//                String filePath, String filename, InputStream input
        try {
            boolean isSuccess = FtpUtil.uploadFile(
                    FTP_URL,
                    Integer.parseInt(FTP_PORT),
                    FTP_USERNAME,
                    FTP_PASSWORD,
                    FTP_BASEPATH,
                    simpleDateFormat.format(new Date()),
                    IDUtil.getStrId()+".jpg",
                    file.getInputStream()
            );
            System.out.println(isSuccess);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 测试下返回值不同是否会引起aop的错误
     * @param apiRequest
     * @return
     */
    @EnableLimitPage
    @Override
    public Map<String, Object> getAllToMap(APIRequest apiRequest) {
        Map<String, Object> map = new HashMap<>();
        map.put("stus",studentMapper.selectByExample(null));
        return map;
    }
}
